package com.xydtech.ecard.config.service.impl;

import com.xydtech.common.annotation.DataScope;
import com.xydtech.common.core.domain.entity.SysDept;
import com.xydtech.common.core.text.Convert;
import com.xydtech.common.utils.AESEncryptionUtils;
import com.xydtech.common.utils.ShiroUtils;
import com.xydtech.common.utils.StringUtils;
import com.xydtech.ecard.config.domain.EcardBusiReportPerson;
import com.xydtech.ecard.config.mapper.EcardBusiAndConfigMapper;
import com.xydtech.ecard.config.mapper.EcardBusiReportPersonMapper;
import com.xydtech.ecard.config.service.IEcardBusiReportPersonService;
import com.xydtech.system.service.ISysDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

/**
 * 电子名片业务填报个人类信息Service业务层处理
 *
 * @author yuzhiguo
 * @date 2024-09-29 下午2:03
 */
@Service
public class EcardBusiReportPersonServiceImpl implements IEcardBusiReportPersonService
{
    @Autowired
    private EcardBusiReportPersonMapper ecardBusiReportPersonMapper;
    @Autowired
    private EcardBusiAndConfigMapper ecardBusiAndConfigMapper;
    @Autowired
    private ISysDeptService sysDeptService;

    /**
     * 查询电子名片业务填报个人类信息
     *
     * @param id 电子名片业务填报个人类信息主键
     * @return 电子名片业务填报个人类信息
     */
    @Override
    public EcardBusiReportPerson selectEcardBusiReportPersonById(Long id, String salt)
    {
        EcardBusiReportPerson busiReportPerson = ecardBusiReportPersonMapper.selectEcardBusiReportPersonById(id);
        if (busiReportPerson != null) {
            try {
                //数据解密（手机号、身份证号）
                if (!StringUtils.objIsNullOrEmpty(busiReportPerson.getPerCustPhone())) {
                    busiReportPerson.setPerCustPhone(AESEncryptionUtils.decrypt(busiReportPerson.getPerCustPhone(), salt));
                }
                if (!StringUtils.objIsNullOrEmpty(busiReportPerson.getPerCustIdcard())) {
                    busiReportPerson.setPerCustIdcard(AESEncryptionUtils.decrypt(busiReportPerson.getPerCustIdcard(), salt));
                }
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
        return busiReportPerson;
    }

    /**
     * 查询电子名片业务填报个人类信息列表
     *
     * @param ecardBusiReportPerson 电子名片业务填报个人类信息
     * @return 电子名片业务填报个人类信息
     */
    @Override
    @DataScope(deptAlias = "d", userAlias = "u")
    public List<EcardBusiReportPerson> selectEcardBusiReportPersonList(EcardBusiReportPerson ecardBusiReportPerson, String salt)
    {
        try {
            //数据加密（手机号、身份证号）
            if (!StringUtils.objIsNullOrEmpty(ecardBusiReportPerson.getPerCustPhone())) {
                ecardBusiReportPerson.setPerCustPhone(AESEncryptionUtils.encrypt(ecardBusiReportPerson.getPerCustPhone(), salt));
            }
            if (!StringUtils.objIsNullOrEmpty(ecardBusiReportPerson.getPerCustIdcard())) {
                ecardBusiReportPerson.setPerCustIdcard(AESEncryptionUtils.encrypt(ecardBusiReportPerson.getPerCustIdcard(), salt));
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        List<EcardBusiReportPerson> busiReportPersonList = ecardBusiReportPersonMapper.selectEcardBusiReportPersonList(ecardBusiReportPerson);
        if (busiReportPersonList.size() > 0) {
            for (EcardBusiReportPerson busiReportPerson : busiReportPersonList) {
                try {
                    //数据解密（手机号、身份证号）
                    if (!StringUtils.objIsNullOrEmpty(busiReportPerson.getPerCustPhone())) {
                        busiReportPerson.setPerCustPhone(AESEncryptionUtils.decrypt(busiReportPerson.getPerCustPhone(), salt));
                    }
                    if (!StringUtils.objIsNullOrEmpty(busiReportPerson.getPerCustIdcard())) {
                        busiReportPerson.setPerCustIdcard(AESEncryptionUtils.decrypt(busiReportPerson.getPerCustIdcard(), salt));
                    }
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }
        }
        return busiReportPersonList;
    }

    /**
     * 新增电子名片业务填报个人类信息
     *
     * @param ecardBusiReportPerson 电子名片业务填报个人类信息
     * @return 结果
     */
    @Override
    public int insertEcardBusiReportPerson(EcardBusiReportPerson ecardBusiReportPerson, String salt)
    {
        try {
            //数据加密（手机号、身份证号）
            if (!StringUtils.objIsNullOrEmpty(ecardBusiReportPerson.getPerCustPhone())) {
                ecardBusiReportPerson.setPerCustPhone(AESEncryptionUtils.encrypt(ecardBusiReportPerson.getPerCustPhone(), salt));
            }
            if (!StringUtils.objIsNullOrEmpty(ecardBusiReportPerson.getPerCustIdcard())) {
                ecardBusiReportPerson.setPerCustIdcard(AESEncryptionUtils.encrypt(ecardBusiReportPerson.getPerCustIdcard(), salt));
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return ecardBusiReportPersonMapper.insertEcardBusiReportPerson(ecardBusiReportPerson);
    }

    /**
     * 修改电子名片业务填报个人类信息
     *
     * @param ecardBusiReportPerson 电子名片业务填报个人类信息
     * @return 结果
     */
    @Override
    public int updateEcardBusiReportPerson(EcardBusiReportPerson ecardBusiReportPerson, String salt)
    {
        try {
            //数据加密（手机号、身份证号）
            if (!StringUtils.objIsNullOrEmpty(ecardBusiReportPerson.getPerCustPhone())) {
                ecardBusiReportPerson.setPerCustPhone(AESEncryptionUtils.encrypt(ecardBusiReportPerson.getPerCustPhone(), salt));
            }
            if (!StringUtils.objIsNullOrEmpty(ecardBusiReportPerson.getPerCustIdcard())) {
                ecardBusiReportPerson.setPerCustIdcard(AESEncryptionUtils.encrypt(ecardBusiReportPerson.getPerCustIdcard(), salt));
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return ecardBusiReportPersonMapper.updateEcardBusiReportPerson(ecardBusiReportPerson);
    }

    /**
     * 批量删除电子名片业务填报个人类信息
     *
     * @param ids 需要删除的电子名片业务填报个人类信息主键
     * @return 结果
     */
    @Override
    public int deleteEcardBusiReportPersonByIds(String ids)
    {
        //循环删除电子名片业务填报信息和业务类型关联表
        for (String busiId : ids.split(",")) {
            ecardBusiAndConfigMapper.deleteEcardBusiAndConfigByConfigIdAndBusiType(busiId, "1");
        }
        return ecardBusiReportPersonMapper.deleteEcardBusiReportPersonByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除电子名片业务填报个人类信息信息
     *
     * @param id 电子名片业务填报个人类信息主键
     * @return 结果
     */
    @Override
    public int deleteEcardBusiReportPersonById(Long id)
    {
        return ecardBusiReportPersonMapper.deleteEcardBusiReportPersonById(id);
    }

    /**
     * 导出电子名片业务填报个人类信息列表
     * @param ecardBusiReportPerson
     * @return
     */
    @Override
    @DataScope(deptAlias = "d", userAlias = "u")
    public List<LinkedHashMap<String, Object>> selectEcardBusiReportPersonListByExport(EcardBusiReportPerson ecardBusiReportPerson, String salt) {
        SysDept sysDept = sysDeptService.selectSecondLevelDeptByDeptId(ShiroUtils.getSysUser().getDeptId());
        ecardBusiReportPerson.setDictDeptId(sysDept.getDeptId());
        List<LinkedHashMap<String, Object>> ecardBusiReportPersons = ecardBusiReportPersonMapper.selectEcardBusiReportPersonListByExport(ecardBusiReportPerson);
        List<LinkedHashMap<String, Object>> ecardBusiReportPersonMaps = new ArrayList<>();
        for (LinkedHashMap<String, Object> busiReportPersonMap : ecardBusiReportPersons) {
            LinkedHashMap<String, Object> maps = new LinkedHashMap<>();
            maps.put("客户姓名", busiReportPersonMap.get("per_cust_name"));
            maps.put("业务名称", busiReportPersonMap.get("busi_name"));
            maps.put("积分", busiReportPersonMap.get("integral_nums"));
            try {
                //数据解密（手机号、身份证号）
                if (!StringUtils.objIsNullOrEmpty(busiReportPersonMap.get("per_cust_phone"))) {
                    maps.put("联系方式", AESEncryptionUtils.decrypt(String.valueOf(busiReportPersonMap.get("per_cust_phone")), salt));
                }
                if (!StringUtils.objIsNullOrEmpty(busiReportPersonMap.get("per_cust_idcard"))) {
                    maps.put("身份证号", AESEncryptionUtils.decrypt(String.valueOf(busiReportPersonMap.get("per_cust_idcard")), salt));
                }
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
            String busiCode = String.valueOf(busiReportPersonMap.get("busi_code"));
            if ("busi_per_social_security".equals(busiCode)){
                maps.put("社保卡类别", busiReportPersonMap.get("per_social_type"));
            } else if ("busi_per_deposit".equals(busiCode)){
                maps.put("存款类型", busiReportPersonMap.get("per_deposit_type"));
                maps.put("存款金额", busiReportPersonMap.get("per_deposit_amount"));
                maps.put("存款期限", busiReportPersonMap.get("per_deposit_term"));
            } else if ("busi_per_loan".equals(busiCode)){
                maps.put("贷款类型", busiReportPersonMap.get("per_loan_type"));
                maps.put("贷款金额", busiReportPersonMap.get("per_loan_amount"));
                maps.put("贷款期限", busiReportPersonMap.get("per_loan_term"));
                maps.put("贷款利率", busiReportPersonMap.get("per_loan_rate"));
            }
            maps.put("营销日期", busiReportPersonMap.get("per_marketing_date"));
            maps.put("备注说明", busiReportPersonMap.get("remark"));
            maps.put("创建时间", busiReportPersonMap.get("creation_time"));
            maps.put("营销经理", busiReportPersonMap.get("user_name"));
            maps.put("员工号", busiReportPersonMap.get("user_code"));
            maps.put("归属机构", busiReportPersonMap.get("dept_name"));
            ecardBusiReportPersonMaps.add(maps);
        }
        return ecardBusiReportPersonMaps;
    }
}
